package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.controller.Controller;
import com.cardbaobao.cardbabyclient.model.TransactionData;
import com.cardbaobao.cardbabyclient.model.result.TransactionDataResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.cardbaobao.cardbabyclient.widget.divider.VerticalItemDecoration;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/11 19:07.
 */

public class TransactionDetailActivity extends BaseActivity {
    public static final String ORDER_NUMBER_S="orderNumber";
    public static final String DETAIL_TYPE_I="type";
    public static final int TYPE_DEBIT=0;//收款详情
    public static final int TYPE_WITHDRAW=1;//提现详情
    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tv_amount)
    TextView tvAmount;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private String orderNumber;
    private List<TransactionData.Bean> data;
    private CommonRecyclerAdapter<TransactionData.Bean> adapter;

    private int detailType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_quick_debit_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBar.setRightViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Controller controller=new Controller(mContext);
                controller.toCommonQuestion(TransactionDetailActivity.this,
                        mCurrentPageId);
            }
        });
        Intent intent=getIntent();
        detailType=intent.getIntExtra(DETAIL_TYPE_I,
                TYPE_DEBIT);
        orderNumber=intent.getStringExtra(ORDER_NUMBER_S);
        if(detailType==TYPE_DEBIT){
            titleBar.setTitleText("收款详情");
            mCurrentPageId= Self.Page.app_debit_detail_page;
        }else{
            titleBar.setTitleText("提现详情");
            mCurrentPageId= Self.Page.app_withdraw_detail_page;
        }

        LinearLayoutManager layoutManager=
                new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        VerticalItemDecoration decoration=new VerticalItemDecoration(Constant.DIVIDER_COLOR,1);
        decoration.setPaddingLeftRight(DensityUtil.dp2px(20),0);
        recyclerView.addItemDecoration(decoration);

        data=new ArrayList<>();
        adapter=new CommonRecyclerAdapter<TransactionData.Bean>(mContext,data,R.layout.item_debit_detail) {
                    @Override
                    public void convert(CommonRecyclerHolder holder, TransactionData.Bean bean) {
                        if(holder.position==0){
                            RecyclerView.MarginLayoutParams params=
                                    (RecyclerView.MarginLayoutParams) holder.itemView.getLayoutParams();
                            params.topMargin= com.cardbaobao.cardbabyclient.utils.DensityUtil.dp2px(13);
                        }
                        holder.setTextViewText(R.id.tv_key,bean.getName());
                        holder.setTextViewText(R.id.tv_value,bean.getValue());
                    }
                };
        recyclerView.setAdapter(adapter);

        getDetails();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(detailType==TYPE_DEBIT){
            GoogleAnalyticsUtil.setScreenName("收款详情");
        }else{
            GoogleAnalyticsUtil.setScreenName("提现详情");
        }
    }

    private void getDetails(){
        showProgressDialog(false);
        Api.getTransactionDetail(String.valueOf(mApplication.getUser().getID()),
                orderNumber).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                TransactionDataResult result= JsonUtil.json2Object(s,TransactionDataResult.class);
                if(result.getCode()==Constant.SUC_CODE){
                    TransactionData.Details details=result.getData().getTransaction_details();
                    if(details!=null){
                        if(detailType==TYPE_DEBIT){
                            //收款
                            tvAmount.setText("￥"+(String.format(Locale.CHINESE,
                                    Constant.AMOUNT_FORMAT,
                                    Double.parseDouble(details.getPayable_amount()))));
                        }else{
                            //提现
                            tvAmount.setText("￥"+(String.format(Locale.CHINESE,
                                    Constant.AMOUNT_FORMAT,
                                    Double.parseDouble(details.getReceive_amount()))));
                        }
                        data.clear();
                        data.addAll(TransactionData.parse(details,detailType));
                        adapter.notifyDataSetChanged();
                    }
                }else{
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }
}
